/*****************************************************************

The GUI code is based on Run applet Copyright (c) 2000 Matthias Elter <elter@kde.org>

Modifications by Sergejs Melderis, 2006 <sergey.melderis@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/



#ifndef KLINGVOTRANSLATOR_H
#define KLINGVOTRANSLATOR_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <kpanelapplet.h>
// for clear input button
#include <kiconloader.h>
#include <qstring.h>
#include <kconfig.h>
#include <kurl.h>
#include <kio/job.h>        
#include "prefs.h"
// for FlatButton
#include "languagechooser.h"
#include <dcopobject.h>
class KHistoryCombo;
class KURL;
class TextEncoder;
class QPopupMenu;
class Browser;
class LanguageChooser;
class QPushButton;
class QHBox;
class BrowserPopupMenu;
class WordQuizWriter;
class KTempDir;
class KLingvoTranslator : public KPanelApplet, public DCOPObject
{
    Q_OBJECT
    K_DCOP

public:
    /**
     * Construct a @ref KPanelApplet just like any other widget.
     *
     * @param configFile The configFile handed over in the factory function.
     * @param Type The applet @ref type().
     * @param actions Standard RMB menu actions supported by the applet (see @ref action() ).
     * @param parent The pointer to the parent widget handed over in the factory function.
     * @param name A Qt object name for your applet.
     **/
    KLingvoTranslator(const QString& configFile, Type t = Normal, int actions = 0,
        QWidget *parent = 0, const char *name = 0);
    /** destructor */
    ~KLingvoTranslator();

    /**
     * Retrieve a suggested width for a given height.
     *
     * Every applet should reimplement this function.
     *
     * Depending on the panel orientation the height (horizontal panel) or the
     * width (vertical panel) of the applets is fixed.
     * The exact values of the fixed size component depend on the panel size.
     *
     * On a horizontal panel the applet height is fixed, the panel will
     * call @ref widthForHeight(int height) with @p height
     * equal to 'the fixed applet height'
     * when laying out the applets.
     *
     * The applet can now choose the other size component (width)
     * based on the given height.
     *
     * The width you return is granted.
     **/
    virtual int widthForHeight(int height) const;
    /**
     * @return A suggested height for a given width.
     *
     * Every applet should reimplement this function.
     *
     * Depending on the panel orientation the height (horizontal panel) or the
     * width (vertical panel) of the applets is fixed.
     * The exact values of the fixed size component depend on the panel size.
     *
     * On a vertical panel the applet width is fixed, the panel will
     * call @ref heightForWidth(int width) with @p width
     * equal to 'the fixed applet width'
     * when laying out the applets.
     *
     * The applet can now choose the other size component (height)
     * based on the given width.
     *
     * The height you return is granted.
     **/
    virtual int heightForWidth(int width) const;
    /**
     * Is called when the user selects "About" from the applets RMB menu.
     * Reimplement this function to launch a about dialog.
     *
     * Note that this is called only when your applet supports the About action.
     * See @ref Action and @ref KPanelApplet().
     **/
    virtual void about();

    /**
     * This enum type describes the page in the configuration dialog.
     **/
    enum ConfigPage { Fonts, Transliteration, KWordQuizFiles};
    
    /**
     * Is called when the user selects "Preferences" from the applets RMB menu.
     * Reimplement this function to launch a preferences dialog or kcontrol module.
     *
     * Note that this is called only when your applet supports the preferences action.
     * See @ref Action and @ref KPanelApplet().
     **/
    virtual void preferences();

    virtual void preferences(int activePage);

k_dcop:
    void translateWordDCOP(const QString &w);
    void translateClipboard();
    
protected:
    /**
     * Is called when the widged is resized. Reimplemented from QWidget.
     * @param e not used
     */
    void resizeEvent(QResizeEvent * /* e */);

    /**
     * Is called when a mouse event is received. Reimplemented from QWidget.
     * @param e the mouse event
     */
    void mousePressEvent(QMouseEvent *e);

public slots:
    /**
     * This slot can be called to change the input and output languages.
     * @param from the language to translate from
     * @param to the language to translate to
     */
    void changeLanguages(const QString &from, const QString &to);

    /**
     * This slot can be called to clear input line.
     */
    void clearInput();

     /**
     * Is called when the user selects "Help" from the applets RMB menu.
     * Reimplement this function to launch a manual or help page.
     *
     * Note that this is called only when your applet supports the Help action.
     * See @ref Action and @ref KPanelApplet().
      **/
    virtual void help();
    
private:
    QPixmap *pmClearInput;
    FlatButton *mClearInputB;
    KHistoryCombo *mInput;
    KURL mUrl;
    QString mPath;
    TextEncoder *mEncoder;
    QPopupMenu *mContextMenu;

    bool transliterate;
    Browser *mBrowser;
    BrowserPopupMenu *mBrowserMenu;

    LanguageChooser *mChooser;
    QString mPatternForCompletion;
    QString mCurWord;
    QString mFromLanguage;
    QString mToLanguage;
    QStringList mLanguageList;
    QPushButton *mBtn;
    QHBox *mHbox;

    WordQuizWriter *mWordWriter;
    KTempDir *mTempDir;

    /**
     * Function returns the language of the input word.
     * It is either Russian of English at the time.
     * @param w input word
     * @return the language
     */
    QString checkInput(const QString &w);

    /**
     * Functin sets the language index that is used for quering Lingvo web site.
     */
    int languageIndex(const QString &text);
private slots:
    /**
     * The slot is called when text is changed in HistoryCombo mInput.
     * @param text the current text in mInput
     */
    void textChanged(const QString &text);

    /**
     * The slot is called then browser finished opening Lingvo web page
     * and if Lingvo has a translation for the current word it adds the
     * word to history.
     */
    void mayBeAddWordToCompletion();

    // May be this is not need....
    void setTranslitMode();

    /**
     * This slot is called when transliterate mode is set and the
     * current input language is Russian. Replaces English characters
     * with "corresponding" Russian characters and sets string to
     * input combo.
     * @param t the text to transliterate
     */
    void transliterateText(const QString &t);

    /**
     * This slot is called when return key is pressed. Sets web query for Lingvo
     * and  passes it to browser.
     * @param w the word to translate.
     */
    void translate(const QString &w);

    /**
     * This slot is called when 'translate clipboard button' pressed.
     * Sets input field with specified text and calls 'translate' function
     * @param w the word to translate.
     */
    void translateClipboard(const QString &w);

    void popupCombo();

    void reconfigure();

    void instantiateBrowser();

    void deleteBrowser();

    void showFirstTimeHelp();

    void prepareHelpHtml(KIO::Job *job);

    void setUpKWordQuizFiles();

};


#endif
